﻿using Architecture.Domain;
using Architecture.Domain.Dto;
using Architecture.Domain.RBAC;
using Architecture.Repository.Interface;
using Architecture.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Service
{
    public class UserService : IUserService
    {
        private readonly IUserRepository userRepository;
        private readonly IProvinceRepository provinceRepository;
        private readonly IUrbanRepository urbanRepository;
        private readonly ICountyRepository countyRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"></param>
        public UserService(IUserRepository userRepository, IProvinceRepository provinceRepository, IUrbanRepository urbanRepository, ICountyRepository countyRepository)
        {
            this.userRepository = userRepository;
            this.provinceRepository = provinceRepository;
            this.urbanRepository = urbanRepository;
            this.countyRepository = countyRepository;
        }
        #region 用户表
        //用户登录
        public async Task<Information> Login(string username, string userpwd)
        {
            Information user = new Information();
            var userlist = await userRepository.GetAllAsync();
            var list = userlist.FirstOrDefault(x => x.UserName == username);
            if (list == null)
            {
                user.Gode = -1;
                user.Message = "用户名不存在";
                return user;
            }
            if (list.UserPwd != userpwd)
            {
                user.Gode = -2;
                user.Message = "用户名和密码不匹配";
                return user;
            }
            user.Gode = list.UserId;
            user.Message = "登录成功";
            return user;
        }


        /// <summary>
        /// 用户表添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(UserInfo model)
        {
            model.AddName = "张三";
            model.AddTime= DateTime.Now;
            return await userRepository.AddAsync(model);
        }
        /// <summary>
        /// 省份表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Province>> GetAllProvince()
        {
            return await provinceRepository.GetAllAsync();
        }
        /// <summary>
        /// 市区表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Urban>> GetAllUrban(int? id)
        {
            var list = await urbanRepository.GetAllAsync();
            if (id != null)
            {
                list = list.Where(x => x.ProvinceId == id).ToList();
            }
            return list;
        }
        /// <summary>
        /// 县城表
        /// </summary>
        /// <returns></returns>
        public async Task<List<County>> GetAlCounty(int? id)
        {
            var list = await countyRepository.GetAllAsync();
            if (id != null)
            {
                list = list.Where(x => x.UrbanId == id).ToList();
            }
            return list;
        }

        /// <summary>
        /// 俩表连显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageListDto<UserListDto>> GetAllAsync(int pageIndex,int pageSize, string? userName)
        {
            PageListDto<UserListDto> page = new PageListDto<UserListDto>();
            var userList = await userRepository.GetAllAsync();
            var provinceList = await provinceRepository.GetAllAsync();
            var urbanList = await urbanRepository.GetAllAsync();
            var countyList = await countyRepository.GetAllAsync();
            var tempList = (from a in userList
                            join b in provinceList on a.ProvinceId equals b.ProvinceId
                            join c in urbanList on a.UrbanId equals c.UrbanId
                            join d in countyList on a.CountyId equals d.CountyId
                            where !a.IsDelete
                            select new UserListDto
                            {
                                UserId = a.UserId,
                                UserName = a.UserName,
                                UserSex = a.UserSex,
                                UserAddress = a.UserAddress,
                                AddName = a.AddName,
                                AddTime = a.AddTime,
                                UpdateName = a.UpdateName,
                                UpdateTime = a.UpdateTime,
                                IsDelete = a.IsDelete,
                                ProvinceId = a.ProvinceId,
                                ProvinceName = b.ProvinceName,
                                UrbanId = a.UrbanId,
                                UrbanName = c.UrbanName,
                                CountyId = a.CountyId,
                                CountyName = d.CountyName,
                            }).ToList();
            if (userName != null)
            {
                tempList = tempList.Where(x=>x.UserName.Contains(userName)).ToList();
            }
            page.ListCount = tempList.Count;
            page.PageCount = (int)Math.Ceiling((page.ListCount * 1.0) / pageSize);
            page.Lists = tempList.OrderByDescending(x => x.UserId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return page;
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            var list = await userRepository.GetAsync(id);
            list.IsDelete = true;
            return await userRepository.UpdateAsync(list);
        }
        /// <summary>
        /// 查询数据详情
        /// </summary>
        /// <param name="id">数据id</param>
        /// <returns>返回数据对象</returns>
        public async Task<UserInfo> GetUserInfo(int id)
        {
            var list = await userRepository.GetAsync(id);
            return list;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(UserInfo model)
        {
            var list = await userRepository.GetAsync(model.UserId);
            list.UserName = model.UserName;
            list.UserAddress = model.UserAddress;
            list.ProvinceId = model.ProvinceId;
            list.UrbanId = model.UrbanId;
            list.CountyId = model.CountyId;
            list.UpdateName = "李四";
            list.UpdateTime = DateTime.Now;
            return await userRepository.UpdateAsync(list);
        }
        /// <summary>
        /// 员工树形绑定
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<object> GetInquire(string ids)
        {
            var list=from a in await userRepository.GetAllAsync() 
                      where !a.IsDelete && ids.Contains(a.SectoriD.ToString())
                      select new
                      {
                          value=a.UserId,
                          label=a.UserName
                      };
            return list;
        }
        #endregion
    }
}
